package com.lyhlmj.oop_client.Frames.PlayFrame;

import com.lyhlmj.oop_client.ClientOperation.ClientUtil.Client;
import com.lyhlmj.oop_client.Frames.PlayFrame.Panels.*;
import com.lyhlmj.oop_client.Utils.CenterUtils;
import com.lyhlmj.oop_client.Utils.ExitUtil;
import com.lyhlmj.oop_client.Utils.GetScreenSize;

import javax.swing.*;
import java.awt.*;

public class PlayFrame extends JFrame {
    public PlayFrame(String title) {
        super(title);

        // 设置主控件为背景Panel
        RootPanel rootPanel = new RootPanel();
        rootPanel.setLayout(new BorderLayout());
        this.setContentPane(rootPanel);

        //设置其余控件的Panel
        ChessBoardPanel chessBoardPanel = new ChessBoardPanel(this);
        rootPanel.add(chessBoardPanel, BorderLayout.CENTER);

        ChatRoomPanel chatRoomPanel = new ChatRoomPanel(this);
        rootPanel.add(chatRoomPanel, BorderLayout.EAST);

        PlayFrameButtonPanels playFrameButtonPanels = new PlayFrameButtonPanels(this, this);
        playFrameButtonPanels.setButtonFunction();
        rootPanel.add(playFrameButtonPanels, BorderLayout.SOUTH);

        //设置获胜关闭的对象
        Client.client.getPlayStatusTask.setFrame(this);

        //设置用户退出对局的操作
        ExitUtil.exitInPlay(this);
    }

    public static void initial() {
        synchronized (Client.client){
            JFrame frame = new PlayFrame(Client.client.getPlayers()[0] + "与" + Client.client.getPlayers()[1] + "的五子棋对局界面");

            //  得到Frame的大小
            int width = GetScreenSize.getFitSize()[0];
            int height = GetScreenSize.getFitSize()[1];

            frame.setSize(width, height);//设置框架的高度和宽度
            frame.dispose();//设置默认关闭方式(只关闭Swing窗口，不关闭程序)
            frame.setVisible(true);//设置可见性

            // 设置框架显示在屏幕正中间
            CenterUtils.centerInScreen(frame);
        }
    }
}
